﻿using System;
using System.Collections.Generic;
using System.Data;
using UFIDA.U9.Base;
using UFSoft.UBF.AopFrame;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business.Lock;
using UFSoft.UBF.Sys.Database;
using UFSoft.UBF.Transactions;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.Cust.Pub.WSLogBP
{
    /// <summary>
    ///     DeleteDBLogBP partial
    /// </summary>
    public partial class DeleteDBLogBP
    {
        internal BaseStrategy Select()
        {
            return new DeleteDBLogBPImpementStrategy();
        }
    }

    #region  implement strategy	

    /// <summary>
    ///     Impement Implement
    /// </summary>
    internal class DeleteDBLogBPImpementStrategy : BaseStrategy
    {
        public override object Do(object obj)
        {
            DeleteDBLogBP bpObj = (DeleteDBLogBP) obj;
            if (bpObj.Date == DateTime.MinValue)
                throw new BpParameterException("DeleteDBLogBP", "Date");
            string lockString = typeof(DeleteDBLogBP).FullName;
            EntityLock el = EntityLockManager.ObjectLockForWrite(lockString, true);
            try
            {
                const string querySql = @"
                    SELECT TOP 100
                           ID
                    FROM dbo.Cust_Pub_WSLog
                    WHERE CreatedOn < @Date
                    ORDER BY CreatedOn";
                DataParamList dataParams = new DataParamList();
                dataParams.Add(DataParamFactory.CreateInput("Date", bpObj.Date, DbType.DateTime));
                while (true)
                {
                    DataSet dataSet;
                    DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), querySql, dataParams, out dataSet);
                    if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
                        break;
                    List<long> logIDs = new List<long>();
                    foreach (DataRow row in dataSet.Tables[0].Rows)
                    {
                        logIDs.Add(AsLong(row, "ID"));
                    }

                    string deleteSql = string.Format("DELETE Cust_Pub_WSLog WHERE ID IN ({0})",
                        string.Join(",", logIDs));
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), deleteSql, null);
                        scope.Commit();
                    }
                }
            }
            finally
            {
                if (el != null)
                {
                    EntityLockManager.Unlock(el);
                }
            }

            return null;
        }

        public static long AsLong(DataRow dataRow, string columnName)
        {
            return AsLong(dataRow, columnName, default(long));
        }

        public static long AsLong(DataRow dataRow, string columnName, long defaultValue)
        {
            object value = dataRow[columnName];
            return IsDBNullOrEmpty(value) ? defaultValue : Convert.ToInt64(value);
        }

        /// <summary>
        ///     DataRow字段是否为DBNull或string.Empty
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDBNullOrEmpty(object value)
        {
            if (Convert.IsDBNull(value)) return true;
            return value.ToString() == string.Empty;
        }
    }

    #endregion
}